/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved.
 *
 */
import { expect } from '@jest/globals';
import fse from 'fs-extra';
import path from 'path';

import { runHvigorAsync } from '../../common/common';
import { commonJson5Operation } from '../../plugins/common-fun';
import {
  AbilityType,
  APIType,
  APIVersion,
  CaseLevel,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType,
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOptionBuilder } from '../../plugins/task-builder/test-task-option';

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API18,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility,
  },
};

const testfun: TestCaseFun = async (project, taskConfig, record) => {
  const projectRootPath = project.projectRootPath();
  await commonJson5Operation(path.resolve(projectRootPath, 'hvigor', 'hvigor-config.json5'), (json) => {
    json['properties'] = json['properties'] || {};
    json['properties']['ohos.uiTransform.Optimization'] = true;
    return json;
  });
  const module = project.getModule('entry');
  const moduleRootPath = module.moduleRootPath();

  const commands: string[] = '--mode module -p product=default -p module=entry@default assembleHap'.split(' ');
  const artifactRoot = path.resolve(moduleRootPath, 'build/default/generated/profile/default/ModuleInfo.ts');

  const result1 = await runHvigorAsync(projectRootPath, commands);
  record.addStep('首次构建，ohos.uiTransform.Optimization为true，CreateHarModuleInfo为Finished，同时有ModuleInfo.ts文件', result1);
  expect(result1.stdout).toContain('BUILD SUCCESSFUL');
  expect(result1.stdout).toContain('Finished :entry:default@CreateModuleInfo');
  expect(fse.existsSync(artifactRoot)).toEqual(true);

  const result2 = await runHvigorAsync(projectRootPath, commands);
  record.addStep('不做任何更改，再次构建，CreateModuleInfo为UP-TO-DATE', result2);
  expect(result2.stdout).toContain('BUILD SUCCESSFUL');
  expect(result2.stdout).toContain('UP-TO-DATE :entry:default@CreateModuleInfo');
  expect(fse.existsSync(artifactRoot)).toEqual(true);

  fse.removeSync(artifactRoot);
  const result5 = await runHvigorAsync(projectRootPath, commands);
  record.addStep('删除产物，再次构建，CreateModuleInfo为Finished', result5);
  expect(result5.stdout).toContain('BUILD SUCCESSFUL');
  expect(result5.stdout).toContain('Finished :entry:default@CreateModuleInfo');
  expect(fse.existsSync(artifactRoot)).toEqual(true);

  await commonJson5Operation(path.resolve(projectRootPath, 'hvigor', 'hvigor-config.json5'), (json) => {
    json['properties'] = json['properties'] || {};
    json['properties']['ohos.uiTransform.Optimization'] = false;
    return json;
  });

  const result3 = await runHvigorAsync(projectRootPath, commands);
  record.addStep('修改工程级ohos.uiTransform.Optimization为false，再次构建，CreateModuleInfo为Finished', result3);
  expect(result3.stdout).toContain('BUILD SUCCESSFUL');
  expect(result3.stdout).toContain('Finished :entry:default@CreateModuleInfo');
  expect(fse.existsSync(artifactRoot)).toEqual(false);

  const result4 = await runHvigorAsync(projectRootPath, commands);
  record.addStep('不做任何更改，再次构建，CreateModuleInfo为Finished', result4);
  expect(result4.stdout).toContain('BUILD SUCCESSFUL');
  expect(result4.stdout).toContain('Finished :entry:default@CreateModuleInfo');
  expect(fse.existsSync(artifactRoot)).toEqual(false);
};

TestCaseBuilder.builder()
  .caseName('CreateModuleInfo')
  .description('CreateModuleInfo任务增量测试用例')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(TestTaskOptionBuilder.builder().caseLevel(CaseLevel.L2).build())
  .testFun(testfun)
  .load();
